﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using OnlineLearningSystem.IRepository;
using OnlineLearningSystem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OnlineLearningSystem.Controllers
{
    /// <summary>
    /// 题目管理
    /// </summary>
    public class TopicController : BaseController
    {
        private readonly IConfiguration _configuration;
        public TopicController(IUserRepository userService, ICourseRepository courseService,
            IBatchRepository batchService, ILearningSituationRepository learningSituationService,
            ITestRepository testService, ITopicRepository topicService, IConfiguration configuration)
            : base(userService, courseService, batchService, learningSituationService, testService, topicService)
        {
            _configuration = configuration;
        }

        /// <summary>
        /// 查询全部题目
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetAll(string typeId, string courseId)
        {
            int code = 0;
            string message = "";
            List<Topic> data = new List<Topic>();
            if ((typeId == "undefined" && courseId == "undefined") || (typeId == null && courseId == null))
            {
                try
                {
                    data = _topicService.GetAll();
                    code = 200;
                    message = "查询成功！";
                }
                catch
                {
                    code = 500;
                    message = "查询失败！";
                    return new JsonResult(new { code, message });
                };

            }
            else if (typeId == "undefined" && courseId != "undefined")
            {
                code = 200;
                message = "查询成功！";
                data = _topicService.GetAll().Where(o => o.course.ID == int.Parse(courseId)).ToList();
            }
            else if (typeId != "undefined" && courseId == "undefined")
            {
                code = 200;
                message = "查询成功！";
                data = _topicService.GetAll().Where(o => o.type == int.Parse(typeId)).ToList();
            }
            else if (typeId != "undefined" && courseId != "undefined")
            {
                code = 200;
                message = "查询成功！";
                data = _topicService.GetAll().Where(o => o.course.ID == int.Parse(courseId) & o.type == int.Parse(typeId)).ToList();
            }
            return new JsonResult(new { code, message, data });
        }


        /// <summary>
        /// 题目添加
        /// </summary>
        /// <param name="topic"></param>
        /// <param name="courseId">课程id</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult TopicCreate(Topic topic, int courseId)
        {
            int code = 0;
            string message = "";
            Course course = _courseService.Get(o => o.ID == courseId);
            if (course == null)
            {
                code = 500;
                message = "科目不存在，请重新选择科目";
                return new JsonResult(new { code, message });
            }
            else
            {
                topic.course = course;
                _topicService.Create(topic);
                code = 200;
                message = "创建成功！";

                return new JsonResult(new { code, message });
            }

        }


        /// <summary>
        /// 题目删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult TopicDelete(int Id)
        {
            int code = 0;
            string message = "";

            Topic topic = new Model.Topic();

            Topic Slist = _topicService.Get(o => o.ID == Id);
            {
                if (Slist == null)
                {
                    code = 500;
                    message = "科目不能为空";
                    return new JsonResult(new { code, message });
                }
                else
                {

                    _topicService.Delete(Slist);

                    code = 200;
                    message = "删除成功！";

                    return new JsonResult(new { code, message });
                }
            }
        }
    }
}
